package org.example.servicefaculty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.model.manage.bean.po.Student;
import org.example.servicefaculty.mapper.StudentMapper;
import org.example.servicefaculty.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import org.example.model.manage.bean.vo.MajorReportRateVO;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> getStudentListByFacultyId(Integer facultyId) {
        return studentMapper.selectList(new QueryWrapper<Student>().eq("faculty_id", facultyId));
    }

    @Override
    public Boolean updateStudentStatus(Integer studentId, Integer status) {
        Student student = new Student();
        student.setId(studentId);
        student.setReportCheck(status);
        return updateById(student);
    }

    @Override
    public Student getStudentById(Integer studentId) {
        return getById(studentId);
    }

    @Override
    public List<Student> getAllByMajorId(Integer majorId) {
        return studentMapper.selectList(new QueryWrapper<Student>().eq("major_id", majorId));
    }

    @Override
    public List<MajorReportRateVO> countByMajor(Integer facultyId) {
        // 获取每个专业的总人数
        List<Map<String, Object>> totalCountList = studentMapper.selectMaps(new QueryWrapper<Student>()
                .eq("faculty_id", facultyId)
                .groupBy("major_id")
                .select("major_id", "count(*) as total_count"));

        // 获取每个专业的报道人数
        List<Map<String, Object>> reportCountList = studentMapper.selectMaps(new QueryWrapper<Student>()
                .eq("faculty_id", facultyId)
                .eq("report_check", 1)
                .groupBy("major_id")
                .select("major_id", "count(*) as report_count"));

        // 转换为VO列表
        List<MajorReportRateVO> result = new ArrayList<>();
        for (Map<String, Object> totalMap : totalCountList) {
            Integer majorId = (Integer) totalMap.get("major_id");
            Long totalCount = (Long) totalMap.get("total_count");

            // 查找对应的报道人数
            Long reportCount = 0L;
            for (Map<String, Object> reportMap : reportCountList) {
                if (majorId.equals(reportMap.get("major_id"))) {
                    reportCount = (Long) reportMap.get("report_count");
                    break;
                }
            }

            // 计算报道率
            double reportRate = totalCount > 0 ? (double) reportCount / totalCount : 0;

            // 创建VO对象
            MajorReportRateVO vo = new MajorReportRateVO();
            vo.setMajorId(majorId);
            vo.setTotalCount(totalCount);
            vo.setReportCount(reportCount);
            vo.setReportRate(reportRate);

            result.add(vo);
        }

        return result;
    }
}